from pattern.circuit import Control


class entry(Control):
	
	def incoming(self, event):
		destination, message = event.data
		bridge = self.acquireInterface(event, "users")
		if bridge is not None:
			users = bridge.pull()
			for username in self.getUserList(event, destination):
				connection = users.get(username, None)
				if connection is not None:
					connection.wfile.write(message + "\r\n")
					connection.wfile.flush()

	def getUserList(self, event, destination):
		bridge = self.acquireInterface(event, "channels")
		if bridge is not None:
			channels = bridge.pull()
			return channels.get(destination, [destination])